﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using cacrb.Models;

namespace cacrb.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            string psd = "CAWBS666PASS2015!";
            DateTime beginTime = DateTime.Parse("2016-01-01");
            DateTime endTime = DateTime.Parse("2017-12-31");
            var db = new cacrmEntities();

            cacrb.dealerBusinessAreajc1.dealerBusinessAreaPortType ser = new dealerBusinessAreajc1.dealerBusinessAreaPortTypeClient();


            cacrb.dealerBusinessAreajc1.getVhclMaterialGroupRequest request =
                               new dealerBusinessAreajc1.getVhclMaterialGroupRequest("90001001", 10000, 1, "CAWBS666", GetMd5Hash(psd), beginTime, endTime);


            dealerBusinessAreajc1.getVhclMaterialGroupResponse response = ser.getVhclMaterialGroup(request);
            
            return Json(response.@return, JsonRequestBehavior.AllowGet);
        }


        public ActionResult Index2()
        {
            string psd = "CAWBS666PASS2015!";
            DateTime beginTime = DateTime.Parse("2016-01-01");
            DateTime endTime = DateTime.Parse("2017-12-31");
            var db = new cacrmEntities();

            cacrb.dealerBusinessAreavc1.dealerBusinessAreaPortType ser = new dealerBusinessAreavc1.dealerBusinessAreaPortTypeClient();


            cacrb.dealerBusinessAreavc1.getVhclMaterialGroupRequest request =
                               new dealerBusinessAreavc1.getVhclMaterialGroupRequest("90001001", 10000, 1, "CAWBS666", GetMd5Hash(psd), beginTime, endTime);


            dealerBusinessAreavc1.getVhclMaterialGroupResponse response = ser.getVhclMaterialGroup(request);
            //return Content(response.@return.Length.ToString());

            return Json(response.@return.Where(o=>o.groupName== "A800"), JsonRequestBehavior.AllowGet);
        }

        public ActionResult ChangeMaterial(string name)
        {
            //查询长安接口
            string psd = "CAWBS666PASS2015!";
            DateTime beginTime = DateTime.Parse("2016-01-01");
            DateTime endTime = DateTime.Parse("2017-12-31"); 
            cacrb.dealerBusinessAreajc1.dealerBusinessAreaPortType ser = new dealerBusinessAreajc1.dealerBusinessAreaPortTypeClient();


            cacrb.dealerBusinessAreajc1.getVhclMaterialGroupRequest request =
                               new dealerBusinessAreajc1.getVhclMaterialGroupRequest("90001001", 10000, 1, "CAWBS666", GetMd5Hash(psd), beginTime, endTime);


            dealerBusinessAreajc1.getVhclMaterialGroupResponse response = ser.getVhclMaterialGroup(request);

            var item = response.@return.FirstOrDefault(o => o.groupName == name);
            if (item!=null)
            {
                var db = new cacrmEntities();

                var info = db.tb_tmvhclmaterialgroup.FirstOrDefault(o => o.groupName == name);
                
                if (info != null)  //修改
                {

                      //info.groupId = item.groupId
                      //info.groupCode = item.groupCode,
                      //info.groupName = item.groupName,
                      //info.status = item.status,
                      //info.parentGroupId = item.parentGroupId,
                      //info.parentGroupCode = item.parentGroupCode,
                      //info.groupLevel = item.groupLevel,
                      //info.currentPage = item.currentPage,
                      //info.totalPage = item.totalPage,
                      //info.flag = item.flag,
                      //info.groupShortName = item.groupShortName,
                      //info.shortStatus = item.shortStatus,
                      //info.dataType = item.dataType,
                      //info.createDate= DateTime.Now,
                      //info.type = "轿车"

                }
                else
                {

                }
            }



            return Content("");

        }

        public ActionResult Drive()
        {
            return View();
        }

        //界面
        public ActionResult Page1()
        {
            return View();
        }
        public ActionResult Page2()
        {
            return View();
        }


        public static string GetMd5Hash(String input)
        {
            if (input == null)
            {
                return null;
            }

            MD5 md5Hash = MD5.Create();

            // 将输入字符串转换为字节数组并计算哈希数据
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // 创建一个 Stringbuilder 来收集字节并创建字符串
            StringBuilder sBuilder = new StringBuilder();

            // 循环遍历哈希数据的每一个字节并格式化为十六进制字符串
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // 返回十六进制字符串
            return sBuilder.ToString();
        }


        //public JsonResult GetBrand()
        //{
        //    var db = new cacrmEntities();

        //    //品牌
        //    var data = (from s in db.data_1_7
        //                where s.status == 10011001 && s.groupLevel == 1
        //                select new { id = s.groupId, text = s.groupName }).Distinct();

        //    return Json(data, JsonRequestBehavior.AllowGet);
        //}
        //public JsonResult GetSeriesByBrand(long id)
        //{
        //    var db = new cacrmEntities();

        //    //车系
        //    var data = (from s in db.data_1_7
        //                where s.status == 10011001 && s.groupLevel == 2 && s.parentGroupId == id
        //                select new { id = s.groupId, text = s.groupName }).Distinct();

        //    return Json(data, JsonRequestBehavior.AllowGet);
        //}

        //public JsonResult GetProvince()
        //{
        //    var db = new cacrmEntities();

        //    //省
        //    var data = (from s in db.data_1_6
        //                where s.regionType == "10541002"
        //                select new { id = s.regionId, text = s.regionName }).Distinct();

        //    return Json(data, JsonRequestBehavior.AllowGet);
        //}

        //public JsonResult GetCityByProvince(string id)
        //{
        //    var db = new cacrmEntities();

        //    //市
        //    var data = (from s in db.data_1_6
        //                where s.regionType == "10541003" && s.parentCode == id
        //                select new { id = s.regionId, text = s.regionName }).Distinct();

        //    return Json(data, JsonRequestBehavior.AllowGet);
        //}

        //public JsonResult GetTownByCity(string id)
        //{
        //    var db = new cacrmEntities();

        //    //区
        //    var data = (from s in db.data_1_6
        //                where s.regionType == "10541004" && s.parentCode == id
        //                select new { id = s.regionId, text = s.regionName }).Distinct();

        //    return Json(data, JsonRequestBehavior.AllowGet);
        //}

        ////public JsonResult GetDealer(string id)
        ////{
        ////    var db = new cacrmEntities();

        ////    //经销商
        ////    var data = (from s in db.data_1_2
        ////                where s.provinceId == 1 && s.cityId == 2&&s.town==3
        ////                select new { id = s.regionId, text = s.regionName }).Distinct();

        ////    return Json(data, JsonRequestBehavior.AllowGet);
        ////}




        //public string SaveSetting(string json)
        //{
        //    try
        //    {
        //        JsonObject obj = new JsonObject(json);
        //        string QD = obj["TXT_QD"].Value.ToString();
        //        string BM = obj["TXT_BM"].Value.ToString();
        //        string NAME = obj["TXT_NAME"].Value.ToString();
        //        string MS = obj["TXT_MS"].Value.ToString();

        //        string CARISSEE = obj["CARISSEE"].Value.ToString();
        //        string _4SISSEE = obj["_4SISSEE"].Value.ToString();
        //        string SJISSEE = obj["SJISSEE"].Value.ToString();
        //        string GCISSEE = obj["GCISSEE"].Value.ToString();

        //        string CARMUST = obj["CARMUST"].Value.ToString();
        //        string _4SMUST = obj["_4SMUST"].Value.ToString();
        //        string SJMUST = obj["SJMUST"].Value.ToString();
        //        string GCMUST = obj["GCMUST"].Value.ToString();

        //        string CARREAD = obj["CARREAD"].Value.ToString();
        //        string _4SRREAD = obj["_4SRREAD"].Value.ToString();
        //        string SJRREAD = obj["SJRREAD"].Value.ToString();
        //        string GCRREAD = obj["GCRREAD"].Value.ToString();

        //        string DDDATE = obj["TXT_DDDATE"].Value.ToString();
        //        string BUYDATE = obj["TXT_BUYDATE"].Value.ToString();
        //        string BRAND = obj["TXT_BRAND"].Value.ToString();
        //        string SERIES = obj["TXT_SERIES"].Value.ToString();


        //        string PROVINCE = obj["TXT_PROVINCE"].Value.ToString();
        //        string CITY = obj["TXT_CITY"].Value.ToString();
        //        string TOWN = obj["TXT_TOWN"].Value.ToString();
        //        string JXS = obj["TXT_JXS"].Value.ToString();

        //        var db = new cacrmEntities();

        //        Base_Setting model = new Base_Setting
        //        {
        //            Channel = QD,
        //            Code = BM,
        //            Name = NAME,
        //            Description = MS,
        //            C_CarShow = CARISSEE,
        //            C_4SShow = _4SISSEE,
        //            C_PlanShow = SJISSEE,
        //            C_BuyShow = GCISSEE,

        //            C_CarMust = CARMUST,
        //            C_4SMust = _4SMUST,
        //            C_PlanMust = SJMUST,
        //            C_BuyMust = GCMUST,

        //            C_CarRead = CARREAD,
        //            C_4SRead = _4SRREAD,
        //            C_PlanRead = SJRREAD,
        //            C_BuyRead = GCRREAD,

        //            C_BuyValue1 = DateTime.Parse(DDDATE),
        //            C_BuyValue2 = DateTime.Parse(BUYDATE),
        //            C_CarValue1 = BRAND,
        //            C_CarValue2 = SERIES,

        //            C_4SValue1 = PROVINCE,
        //            C_4SValue2 = CITY,
        //            C_4SValue3 = TOWN,
        //            C_4SValue4 = JXS,
        //            CreateTime = DateTime.Now
        //        };
        //        db.Base_Setting.Add(model);
        //        if (db.SaveChanges() > 0)
        //            return "success";
        //        else return "保存失败！";

        //    }
        //    catch (Exception ex)
        //    {
        //        return ex.Message;
        //    }
        //}

    }
}
